#!/bin/bash
#SBATCH --spread-job
# Copyright (c) 2018 Presto Labs Pte. Ltd.

export OUTDIR="{{out_dir}}"
mkdir -p "${OUTDIR}"

if [ -z ${SLURM_JOB_ID} ]; then
  (>&2 echo "ERROR: run this script on slurm.")
  exit 1
fi

set -e
source /remote/iosg/home/bot-hft/miniconda3/etc/profile.d/conda.sh
conda activate /remote/iosg/home/bot-hft/miniconda3/envs/coin_env

NUM_PARALLEL="$((${SLURM_NTASKS} + 30))"
parallel -j "${NUM_PARALLEL}" --delay {{job_submit_period}} <<EOF
{% for trading_date in trading_dates -%}
{% for machine in machines -%}
{% for market_type, exchange, api_version in exchange_list -%}
srun -N1 -n1 -l --exclusive ./pyrunner coin/support/feed_tool/feed_stats/app/feed_stats_slurm/feed_stats_single_process_runner.py --trading_date="{{trading_date}}" --machine="{{machine}}" --market_type="{{market_type}}" --exchange="{{exchange}}" --api_version="{{api_version}}" --db_config="{{db_config}}" --git_commit="{{git_commit}}"
{% endfor %}
{%- endfor %}
{%- endfor %}
EOF

python -m coin.tool.mailer --name="coin-feed-stats" --mailfrom="coin-sim@joomo.io" --mailto="{{summary_mailto}}" --subject="{{mail_subject}}" <<EOF
Slurm job ID:     ${SLURM_JOB_ID}
Output directory: ${OUTDIR}
User tag:         None

---
$(scontrol show job ${SLURM_JOB_ID})
EOF
